package com.jiazhong.ssm.test01整合.controller;

import com.jiazhong.ssm.test01整合.pojo.Schedule;
import com.jiazhong.ssm.test01整合.service.ScheduleService;
import com.jiazhong.ssm.test01整合.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther:shuXin
 */
@Slf4j
@RestController
@RequestMapping("schedule")
@CrossOrigin //运行其他域中进行访问
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 按照分页查询数据
     *
     * @param pageSize
     * @param currentPage
     * @return
     */
    @GetMapping("/{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize,
                  @PathVariable int currentPage) {
        R page = scheduleService.page(pageSize, currentPage);
        log.info("分页数据：{}", page);
        return page;
    }

    /**
     * 添加对象的数据
     *
     * @param schedule
     * @return
     */
    @PostMapping
    public R save(@Validated @RequestBody Schedule schedule, BindingResult bindingResult) {
        //进行判断
        if (bindingResult.hasErrors()) {
            return R.fail("参数为空，不能保存");
        }
        R save = scheduleService.save(schedule);
        return save;
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable int id) {
        R delete = scheduleService.delete(id);
        return delete;
    }

    @PutMapping
    public R update(@Validated @RequestBody Schedule schedule, BindingResult result) {
        //进行判断
        if (result.hasErrors()) {
            return R.fail("参数为空，不能修改");
        }
        R update = scheduleService.update(schedule);
        return update;
    }
}
